package com.lanou.dllo.findfood.database;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.lanou.dllo.findfood.entity.User;
import com.lanou.dllo.findfood.util.Constant;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by dllo on 15/11/2.
 */
public class UserDataBaseUtil {
    private SQLiteDatabase database;

    public  UserDataBaseUtil(Context context){
        init(context);
    }

    private void init(Context context) {
        MySQLiteOpenHelper helper = new MySQLiteOpenHelper(context, Constant.DATABASE_NAME,null,1);
        database = helper.getWritableDatabase();
    }

    //保存用户数据
    public  void saveUserData(List<User> userList){
        for (User user : userList){
            ContentValues values = new ContentValues();
            values.put(Constant.COLUMN_USER_NAME,user.getNumber());
            values.put(Constant.COLUMN_USER_PASSWORD,user.getPassword());
            database.insert(Constant.TABLE_NAME_USER, null, values);
            database.close();
        }

    }
   public void deleUser(){
        database.delete(Constant.TABLE_NAME_USER, null, null);
    }

    public List<User> querUser(){
        Cursor cursor = database.query(Constant.TABLE_NAME_USER,null,null,null,null,null,null);
//        String sql2 = "select * from " + Constant.TABLE_NAME_USER
//                + " where _msgId > " + msgId;
//        Cursor cursor = database.rawQuery(sql2, null);
        if (cursor != null){
            List<User> users = new ArrayList<User>();
        while (cursor.moveToNext()){
            String number = cursor.getString(cursor.getColumnIndex(Constant.COLUMN_USER_NAME));
            String password = cursor.getString(cursor.getColumnIndex(Constant.COLUMN_USER_PASSWORD));
            User user = new User();
            user.setNumber(number);
            user.setPassword(password);
            users.add(user);
        }
        cursor.close();
            return users;
        }
        return  null;
    }



}
